﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using CCF20Model;
using System.Globalization;
using CCF20Model.ConfigurationFile;
using System.Threading;


namespace ZICMonitor
{
    
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        int exceptionCount = 0;
        private void Application_Startup(object sender, StartupEventArgs e)
        {

            if (e.Args.Count() > 0)
            {
                ZICMonitorModel.ConfigurationPath = e.Args[0];
                LoggerHelper.LogZICMonitor(string.Format("Configuration File Path ={0}", ZICMonitorModel.ConfigurationPath));
                
            }

            CultureInfo ci;
            if ((bool)ConfiguraitonHelper.MyConfiguration.isenglish)
            {
                ci = new CultureInfo("en");
                LoggerHelper.LogZICMonitor(string.Format("ZICMonitor Culture = English"));
            }
            else
            {
                ci = new CultureInfo("zh");
                LoggerHelper.LogZICMonitor(string.Format("ZICMonitor Culture = Chinese"));
            }
            WiseLink.RDS.Resource.ButtonString.Culture = ci;
            WiseLink.RDS.Resource.CustomerTableString.Culture = ci;
            WiseLink.RDS.Resource.WiseLinkRDSStatus.Culture = ci;
            WiseLink.RDS.Resource.Message.Culture = ci;

            Thread.CurrentThread.CurrentUICulture = ci;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);

        }
        
        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            exceptionCount++;
            if (exceptionCount > 11)
            {
                App.Current.Shutdown();
            }
            LoggerHelper.LogZICMonitor(string.Format("[Exception:]{0} \t [Stack:] {1}",
                                            e.Exception.Message,
                                            e.Exception.StackTrace));

            if (e.Exception.InnerException != null)
            {
                LoggerHelper.LogZICMonitor(string.Format("[Inner Exception:]{0} \t [Stack:] {1}",
                    e.Exception.InnerException.Message,
                    e.Exception.InnerException.StackTrace??""));
            }
            e.Handled = true;
            MessageBox.Show("ZICMontior 运行出现意外。C:\\CCFLog下所有日志文件发送给智信通！","WiseLink", MessageBoxButton.OK);

        }

        private void Application_Exit(object sender, ExitEventArgs e)
        {
            LoggerHelper.FlushLog();

        }
        
    }
}
